/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.owners;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.swing.JOptionPane;
import com.pet.PetinfoBean;

/**
 *
 * @author Administrator
 */
public class ModifyOwnersServlet extends HttpServlet {
     public void wrong1(){    //对话框提示信息
        String msg="输入信息不能为空，请重新输入！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    public void right(){    //对话框提示信息
        String msg="信息修改成功！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
     protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String petname=new String(request.getParameter("petname").getBytes("ISO-8859-1"),"UTF-8");
        String address=new String(request.getParameter("address").getBytes("ISO-8859-1"),"UTF-8");
        String city=new String(request.getParameter("city").getBytes("ISO-8859-1"),"UTF-8");
        String telephone=new String(request.getParameter("telephone").getBytes("ISO-8859-1"),"UTF-8");
        if(petname.length()==0||address.length()==0||city.length()==0||telephone.length()==0){
            wrong1();
            response.sendRedirect("http://localhost:8084/jspsxxxx/luru/chanxunzhanhsi.jsp");
        }
        else{
            try{
                Connection con=null;
                Statement stmt=null;
                Statement stmt1=null;
//                Statement stmt2=null;
                ResultSet rs=null;
//                ResultSet rs1=null;
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/abin? useUnicode=true&characterEncoding=gbk";
                con=DriverManager.getConnection(url,"root","admin");
                stmt=con.createStatement();
                stmt1=con.createStatement();
//                stmt2=con.createStatement();
                HttpSession session=request.getSession();
                String ownername=null;
                ArrayList ownerlist1=(ArrayList) session.getAttribute("ownerlist");
                if(ownerlist1==null||ownerlist1.size()==0){
                    response.sendRedirect("http://localhost:8084/jspsxxxx/luru/chanxunzhanhsi.jsp");
                }else{
                    for(int i=ownerlist1.size()-1;i>=0;i--){
                        OwnersBean hh=(OwnersBean)ownerlist1.get(i);
                        ownername=hh.getOname();
                    }
                }
                String sql1="update t_owners set address='"+address+"',city='"+city+"',telephone='"+telephone+"' where oname='"+ownername+"'";
                stmt.executeUpdate(sql1);
                String sql2="select * from t_owners,t_pets where oname='"+ownername+"' and owner_name='"+ownername+"'";
                rs=stmt1.executeQuery(sql2);
//                HttpSession session=request.getSession();
                    ArrayList petlist =null;
                    petlist=new ArrayList();
                    ArrayList ownerlist=null;
                    ownerlist=new ArrayList();
                    while(rs.next()){
                        OwnersBean ff=new OwnersBean();
                        PetinfoBean dd=new PetinfoBean();
                        ff.setId(rs.getString("id"));
                        ff.setOname(rs.getString("oname"));
                        ff.setAddress(rs.getString("address"));
                        ff.setCity(rs.getString("city"));
                        ff.setTelephone(rs.getString("telephone"));
                        ff.setPetname(rs.getString("name"));
                        dd.setId(rs.getString("petid"));
                        dd.setPetname(rs.getString("name"));
                        dd.setBirthdate(rs.getString("birth_date"));
                        dd.setType(rs.getString("type"));
                        dd.setOwnerid(rs.getString("owner_name"));
                        ownerlist.add(ff);
                        session.setAttribute("ownerlist",ownerlist);
                        petlist.add(dd);
                        session.setAttribute("petlist",petlist);
                        right();
                        response.sendRedirect("http://localhost:8084/jspsxxxx/luru/chanxunzhanhsi.jsp"); 
                    } 
                
                rs.close();
                stmt.close();
                con.close();
//                right();
//                response.sendRedirect("http://localhost:8084/jspsx/owner/lookownerinfo.jsp"); 
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
      protected void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        doGet(request,response);
    }

   
}
